package News.service;

import javax.swing.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Server extends NewsServer{

    public List<Map> selectUserMsg(String sql){
        List<Map> list=new ArrayList<Map>();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            conn=getConn();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();

            while(rs.next()){
                Map map=new HashMap();
                map.put("state",rs.getString(1));
                map.put("title",rs.getString(2));
                map.put("content",rs.getString(3));
                map.put("create_time",rs.getString(4));
                map.put("msgId",rs.getString(5));
                map.put("username",rs.getString(6));
                map.put("fromto",rs.getString(7));
                map.put("sendto",rs.getString(8));
                list.add(map);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,rs);
        }
        return list;
    }

    public String selectUsername(String sql){
        List<Map> list=new ArrayList<Map>();
        PreparedStatement ps=null;
        ResultSet rs=null;
        String uesrName="";
        try{
            conn=getConn();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();

            while(rs.next()){
                uesrName=rs.getString(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,rs);
        }
        return uesrName;
    }

    public int update(String sql) {
        int num=0;
        PreparedStatement ps=null;
        conn=getConn();
        try{
            ps=conn.prepareStatement(sql);
            num=ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,null);
        }
        return num;
    }

    public static void main(String[] args) {
        String[] content = "1234567890c".split("", 11);
        for(int i=0;i<10;i++){
            System.out.println(content[i]);;
        }
    }

}
